package game.targets.entity;

import game.targets.Locator;

import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g3d.Environment;
import com.badlogic.gdx.graphics.g3d.Material;
import com.badlogic.gdx.graphics.g3d.Model;
import com.badlogic.gdx.graphics.g3d.ModelBatch;
import com.badlogic.gdx.graphics.g3d.ModelInstance;
import com.badlogic.gdx.graphics.g3d.attributes.BlendingAttribute;
import com.badlogic.gdx.math.Vector3;

public class GraphicsComponent {

	private Model model;
	private ModelInstance instance;
	private Material material;
	private BlendingAttribute blend;
	private boolean invisible;

	public GraphicsComponent(Vector3 pos, Vector3 rotation, String path, float scale) {
		if (path != null) {
			model = Locator.getAssets().getManager().get(path, Model.class);
			instance = new ModelInstance(model);
			instance.transform.rotate(rotation, 90);
			instance.transform.scl(scale);
			material = instance.materials.get(0);
			instance.transform.setTranslation(pos);
			blend = new BlendingAttribute(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
		} else {
			/*
			 * material = new Material(ColorAttribute.createDiffuse(Color.RED));
			 * ModelBuilder modelBuilder = new ModelBuilder(); model =
			 * modelBuilder.createBox(1f, 1f, 1f, material, Usage.Position |
			 * Usage.Normal); instance = new ModelInstance(model);
			 * instance.transform.scl(scale);
			 * instance.transform.setTranslation(pos);
			 * instance.transform.rotate(rotation, 90); blend = new
			 * BlendingAttribute(GL20.GL_SRC_ALPHA,
			 * GL20.GL_ONE_MINUS_SRC_ALPHA);
			 */
			model = null;
		}
	}

	public void render(Entity entity, ModelBatch batch, Environment environment) {
		if (model != null && !invisible) {
			instance.transform.setTranslation(entity.getPosition());
			batch.render(instance, environment);
		}
	}

	public void renderFade(Entity entity, ModelBatch batch, Environment environment, float alpha) {
		if (model != null && !invisible) {
			blend.opacity = alpha;
			material.set(blend);
			this.render(entity, batch, environment);
		}
	}

	public void setPosition(Vector3 position) {
		instance.transform.setTranslation(position);
	}

	public void rotate(Vector3 rotation, float angle) {
		instance.transform.rotate(rotation, angle);
	}

	public void setInvisible(boolean i) {
		invisible = i;
	}

	public void dispose() {
		if (model != null) {
			model.dispose();
		}
	}

	public void renderAnimated() {

	}

	public ModelInstance getInstance() {
		return instance;
	}

	public void setInstance(ModelInstance instance) {
		this.instance = instance;
	}

	public void setScale(float s) {
		instance.transform.scl(s);
	}

	public void setScale(float x, float y, float z) {
		instance.transform.scl(x, y, z);
	}

}
